<?php
namespace app\common\components;

use yii\web\Controller;
use yii\web\Cookie;

class BaseWebController extends Controller
{
    /**
     * 集成常用公用方法 提供给所有的controller使用
     * get,post,setCookie,getCookie,removeCookie,renderJson
     */
    public $enableCsrfValidation = false;//关闭csrf
    //获取http的get参数
    public function get( $key,$default_val="" )
    {
        $request = \Yii::$app->request;
        return $request->get( $key,$default_val );
    }
    //获取http的post参数
    public function post( $key,$default_val="" )
    {
        return \Yii::$app->request->post( $key,$default_val );
    }

    //设置cookie值
    public function setCookie( $name,$value,$expire = 0 )
    {
        $cookies = \Yii::$app->response->cookies;
        $cookies->add(new Cookie([
            'name'=>$name,
            'value'=>$value,
            'expire'=>$expire,

        ]));
    }

    //获取cookie值
    public function getCookie( $name,$default_val = "" )
    {
        $cookies = \Yii::$app->request->cookies;
        return $cookies->getValue( $name,$default_val );
    }

    //删除cookie
    public function removeCookie( $name )
    {
        $cookies = \Yii::$app->response->cookies;
        $cookies->remove( $name );
    }

    //api统一返回json格式方法
    public function renderJson( $data = [],$msg = 'ok',$code = 200 )
    {
        header( "content-type:apllication/json" );
        echo json_encode([
            'code' =>$code,
            'msg' =>$msg,
            'data'=>$data,
            'req_id' =>uniqid(),

        ]);
    }

    //提示js
    public function renderJs( $msg,$url )
    {
       return $this->renderPartial( '@app/views/common/js',['msg'=>$msg,'url'=>$url] );
    }

}